﻿using Assessment.BLL;
using Assessment.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index_MedicalRecord_MedicalRecordEdit : BasePage_Index
{
    #region 属性
    /// <summary>
    /// Id
    /// </summary>
    public int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }

    /// <summary>
    /// EditType
    /// </summary>
    private string EditType
    {
        get
        {
            return ViewState["EditType"] == null ? "" : ViewState["EditType"].ToString();
        }
        set
        {
            ViewState["EditType"] = value;
        }
    }
    #endregion

    T_MedicalRecord model = new T_MedicalRecord();
    T_MedicalRecordBLL bll = new T_MedicalRecordBLL();

    T_Assessment assessment = new T_Assessment();
    T_AssessmentBLL assessment_bll = new T_AssessmentBLL();

    T_ParameterBLL param_bll = new T_ParameterBLL();

    //T_GaugeRecordBLL gRecord_bll = new T_GaugeRecordBLL();

    UtilityBLL ubll = new UtilityBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        //base.CheckUserPermission("001001");
        if (!IsPostBack)
        {
            base.SetBtnGotoBackUrl("/Index/MedicalRecord/MedicalRecordList.aspx");

            Id = base.ParseQueryInt("Id", 0);
            Action = base.ParseQueryString("Action", "Add");

            BindAllData();

            if (Action == "Modify" || Action == "Detail")
            {
                getDate(Id);

                BindGaugeRecordRepeater(Id);

                GetViewData();

                this.pnl_BaseInfo_Edit.Visible = false;
                this.pnl_BaseInfo_View.Visible = true;

                this.pnl_Hospitalized_Edit.Visible = false;
                this.pnl_Hospitalized_View.Visible = true;

                this.pnl_Diagnosis_Edit.Visible = false;
                this.pnl_Diagnosis_View.Visible = true;

                this.pnl_Assessment_Edit.Visible = false;
                this.pnl_Assessment_View.Visible = true;

                this.btn_Submit.Visible = false;
            }


            EditType = base.ParseQueryString("EditType", "");
            switch (EditType)
            {
                case "BaseInfo":
                    this.pnl_BaseInfo_Edit.Visible = true;
                    this.pnl_BaseInfo_View.Visible = false;
                    this.lbtn_BaseInfo_Edit.Visible = false;
                    this.lbtn_BaseInfo_Save.Visible = true;
                    this.lbtn_BaseInfo_Cancel.Visible = true;
                    break;
                case "Hospitalized":
                    this.pnl_Hospitalized_Edit.Visible = true;
                    this.pnl_Hospitalized_View.Visible = false;
                    this.lbtn_Hospitalized_Edit.Visible = false;
                    this.lbtn_Hospitalized_Save.Visible = true;
                    this.lbtn_Hospitalized_Cancel.Visible = true;
                    break;
                case "Diagnosis":
                    this.pnl_Diagnosis_Edit.Visible = true;
                    this.pnl_Diagnosis_View.Visible = false;
                    this.lbtn_Diagnosis_Edit.Visible = false;
                    this.lbtn_Diagnosis_Save.Visible = true;
                    this.lbtn_Diagnosis_Cancel.Visible = true;
                    break;
                case "Assessment":
                    this.pnl_Assessment_Edit.Visible = true;
                    this.pnl_Assessment_View.Visible = false;
                    this.lbtn_Assessment_Edit.Visible = false;
                    this.lbtn_Assessment_Save.Visible = true;
                    this.lbtn_Assessment_Cancel.Visible = true;
                    break;
            }
        }
    }
    #endregion

    #region 绑定下拉框
    private void BindAllData()
    {
        //绑定 科室
        this.ddl_Department.DataSource = param_bll.GetList("Valid=1 and ParentId=(select top(1) ParameterId from T_Parameter where Value='001')");
        this.ddl_Department.DataTextField = "Title";
        this.ddl_Department.DataValueField = "Value";
        this.ddl_Department.DataBind();
        this.ddl_Department.Items.Insert(0, new ListItem("----请选择----", ""));

        //绑定 临床诊断
        this.cbl_ClinicalDiagnosis.DataSource = param_bll.GetList("Valid=1 and ParentId=(select top(1) ParameterId from T_Parameter where Value='002')");
        this.cbl_ClinicalDiagnosis.DataTextField = "Title";
        this.cbl_ClinicalDiagnosis.DataValueField = "Value";
        this.cbl_ClinicalDiagnosis.DataBind();

        //绑定 特殊情况
        this.cbl_SpecialCircumstances.DataSource = param_bll.GetList("Valid=1 and ParentId=(select top(1) ParameterId from T_Parameter where Value='003')");
        this.cbl_SpecialCircumstances.DataTextField = "Title";
        this.cbl_SpecialCircumstances.DataValueField = "Value";
        this.cbl_SpecialCircumstances.DataBind();
    }
    #endregion

    #region 绑定数据
    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    private void getDate(int id)
    {
        model = bll.GetModel(id);
        if (model == null)
        {
            base.Alert("获取数据失败！");
            return;
        }
        this.txt_BedNumber.Text = model.BedNumber;
        this.txt_BirthDay.Text = model.Birthday;
        this.txt_CertificateNo.Text = model.CertificateNo;
        ShowCheckBoxList(this.cbl_ClinicalDiagnosis, model.ClinicalDiagnosis);
        this.ddl_Department.SelectedValue = model.Department;
        this.txt_Doctor.Text = model.Doctor;
        this.txt_FunctionalDiagnosis.Text = model.FunctionalDiagnosis;
        this.rbl_Gender.SelectedValue = model.Gender.ToString();
        this.rbl_Handedness.SelectedValue = model.Handedness.ToString();
        this.txt_HomeAddress.Text = model.HomeAddress;
        this.txt_Name.Text = model.Name;
        this.txt_Phone.Text = model.Phone;
        this.txt_PlaceOfOrigin.Text = model.PlaceOfOrigin;
        this.txt_RecordNumber.Text = model.RecordNumber;
        this.txt_Remark.Text = model.Remark;
        this.txt_Therapist.Text = model.Therapist;

        assessment = assessment_bll.GetModelList("Valid=1 and MedicalRecordId=" + id).FirstOrDefault();
        if (assessment == null)
        {
            return;
        }
        this.txt_Abstract1.Text = assessment.Abstract1;
        this.txt_Abstract2.Text = assessment.Abstract2;
        this.txt_Abstract3.Text = assessment.Abstract3;
        this.txt_Abstract4.Text = assessment.Abstract4;
        this.txt_Abstract5.Text = assessment.Abstract5;
        this.txt_Abstract6.Text = assessment.Abstract6;
        this.txt_Abstract7.Text = assessment.Abstract7;
        this.txt_Abstract8.Text = assessment.Abstract8;
        this.txt_Abstract9.Text = assessment.Abstract9;
        this.txt_Abstract10.Text = assessment.Abstract10;
        this.txt_Abstract11.Text = assessment.Abstract11;
        this.txt_Abstract12.Text = assessment.Abstract12;
        this.txt_ImagingExamination.Text = assessment.ImagingExamination;
        this.txt_PhysicalExamination.Text = assessment.PhysicalExamination;
        this.txt_Report.Text = assessment.Report;
        ShowCheckBoxList(this.cbl_SpecialCircumstances, assessment.SpecialCircumstances);
    }

    private void GetViewData()
    {
        //-------------
        this.lbl_Name.Text = this.txt_Name.Text;
        this.lbl_Gender.Text = this.rbl_Gender.SelectedItem.Text;
        this.lbl_BirthDay.Text = this.txt_BirthDay.Text;
        this.lbl_Phone.Text = this.txt_Phone.Text;
        this.lbl_PlaceOfOrigin.Text = this.txt_PlaceOfOrigin.Text;
        this.lbl_Handedness.Text = this.rbl_Handedness.SelectedItem.Text;
        this.lbl_CertificateNo.Text = this.txt_CertificateNo.Text;
        this.lbl_HomeAddress.Text = this.txt_HomeAddress.Text;
        //入院信息
        this.lbl_BedNumber.Text = this.txt_BedNumber.Text;
        this.lbl_RecordNumber.Text = this.txt_RecordNumber.Text;
        this.lbl_Department.Text = this.ddl_Department.SelectedItem.Text;
        this.lbl_Doctor.Text = this.txt_Doctor.Text;
        this.lbl_Therapist.Text = this.txt_Therapist.Text;
        this.lbl_Remark.Text = this.txt_Remark.Text;
        //诊断
        this.lbl_ClinicalDiagnosis.Text = GetShowHtmlByCheckBoxList(this.cbl_ClinicalDiagnosis);
        this.lbl_FunctionalDiagnosis.Text = this.txt_FunctionalDiagnosis.Text;
        //基本评估
        this.lbl_Abstract1.Text = this.txt_Abstract1.Text;
        this.lbl_Abstract2.Text = this.txt_Abstract2.Text;
        this.lbl_Abstract3.Text = this.txt_Abstract3.Text;
        this.lbl_Abstract4.Text = this.txt_Abstract4.Text;
        this.lbl_Abstract5.Text = this.txt_Abstract5.Text;
        this.lbl_Abstract6.Text = this.txt_Abstract6.Text;
        this.lbl_Abstract7.Text = this.txt_Abstract7.Text;
        this.lbl_Abstract8.Text = this.txt_Abstract8.Text;
        this.lbl_Abstract9.Text = this.txt_Abstract9.Text;
        this.lbl_Abstract10.Text = this.txt_Abstract10.Text;
        this.lbl_Abstract11.Text = this.txt_Abstract11.Text;
        this.lbl_Abstract12.Text = this.txt_Abstract12.Text;
        this.lbl_Report.Text = this.txt_Report.Text;
        this.lbl_PhysicalExamination.Text = this.txt_PhysicalExamination.Text;
        this.lbl_ImagingExamination.Text = this.txt_ImagingExamination.Text;
        this.lbl_SpecialCircumstances.Text = GetShowHtmlByCheckBoxList(this.cbl_SpecialCircumstances);
    }

    #endregion

    #region 绑定评估记录
    private void BindGaugeRecordRepeater(int MedicalRecordId)
    {
        //List<T_GaugeRecord> list = gRecord_bll.GetModelList("Valid=1 and MedicalRecordId=" + MedicalRecordId);
        int rCount=0;
        DataTable dt = ubll.GetListByPage("T_GaugeRecord", "*", "CreateDate", "Valid=1 and MedicalRecordId=" + MedicalRecordId, 10000, 1, 0, out rCount);
        this.rpt_GaugeRecord.DataSource = dt;
        this.rpt_GaugeRecord.DataBind();
    }
    #endregion

    #region 提交
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        //检验数据是否重复
        //string strCheck = string.Format("(Account like '{0}') and UserId<>{1}",
        //    TextUtility.ReplaceText(this.txt_Account.Text.Trim()),
        //    Id.ToString());

        //if (bll.GetRecordCount(strCheck) > 0)
        //{
        //    base.Alert("模块名称或模块编码已存在！");
        //    return;
        //}

        if (Action == "Modify" || Action == "Detail")
        {
            model = bll.GetModel(Id);
            assessment = assessment_bll.GetModelList("MedicalRecordId=" + Id).FirstOrDefault();
            if (assessment == null)
            {
                assessment = new T_Assessment();
                assessment.AssessmentId = -1;
            }
        }
        else if (Action == "Add")
        {
            model = new T_MedicalRecord();
            assessment = new T_Assessment();
        }
        else
        {
            base.Alert("异常操作！" + Action);
            return;
        }
        model.BedNumber = this.txt_BedNumber.Text.Trim();
        model.Birthday = this.txt_BirthDay.Text.Trim();
        model.CertificateNo = this.txt_CertificateNo.Text.Trim();
        model.ClinicalDiagnosis = "";
        model.ClinicalDiagnosis = GetCheckBoxListValues(this.cbl_ClinicalDiagnosis);
        model.Department = this.ddl_Department.SelectedValue;
        model.Doctor = this.txt_Doctor.Text.Trim();
        model.FunctionalDiagnosis = this.txt_FunctionalDiagnosis.Text.Trim();
        model.Gender = Convert.ToInt32(this.rbl_Gender.SelectedValue);
        model.Handedness = Convert.ToInt32(this.rbl_Handedness.SelectedValue);
        model.HomeAddress = this.txt_HomeAddress.Text.Trim();
        //model.MedicalRecordId
        model.Name = this.txt_Name.Text.Trim();
        model.Phone = this.txt_Phone.Text.Trim();
        model.PlaceOfOrigin = this.txt_PlaceOfOrigin.Text.Trim();
        model.RecordNumber = this.txt_RecordNumber.Text.Trim();
        model.Remark = this.txt_Remark.Text.Trim();
        model.Therapist = this.txt_Therapist.Text.Trim();
        model.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.UpdateUserId = base.CurrentSession.UserID;
        model.Valid = 1;

        assessment.Abstract1 = this.txt_Abstract1.Text.Trim();
        assessment.Abstract2 = this.txt_Abstract2.Text.Trim();
        assessment.Abstract3 = this.txt_Abstract3.Text.Trim();
        assessment.Abstract4 = this.txt_Abstract4.Text.Trim();
        assessment.Abstract5 = this.txt_Abstract5.Text.Trim();
        assessment.Abstract6 = this.txt_Abstract6.Text.Trim();
        assessment.Abstract7 = this.txt_Abstract7.Text.Trim();
        assessment.Abstract8 = this.txt_Abstract8.Text.Trim();
        assessment.Abstract9 = this.txt_Abstract9.Text.Trim();
        assessment.Abstract10 = this.txt_Abstract10.Text.Trim();
        assessment.Abstract11 = this.txt_Abstract11.Text.Trim();
        assessment.Abstract12 = this.txt_Abstract12.Text.Trim();
        assessment.ImagingExamination = this.txt_ImagingExamination.Text.Trim();
        //assessment.MedicalRecordId = 0;
        assessment.PhysicalExamination = this.txt_PhysicalExamination.Text.Trim();
        assessment.Report = this.txt_Report.Text.Trim();
        assessment.SpecialCircumstances = GetCheckBoxListValues(this.cbl_SpecialCircumstances);

        assessment.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        assessment.UpdateUserId = base.CurrentSession.UserID;
        assessment.Valid = 1;

        try
        {
            if (Action == "Add")
            {
                //保存
                model.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                model.CreateUserId = base.CurrentSession.UserID;
                Id = bll.Add(model);

                if (Id > 0)
                {
                    assessment.MedicalRecordId = Id;
                    assessment.CreateUserId = assessment.UpdateUserId;
                    assessment.CreateDate = assessment.UpdateDate;

                    assessment_bll.Add(assessment);
                }
            }
            else
            {
                //修改
                bll.Update(model);
                if (assessment.AssessmentId < 0)
                {
                    assessment.MedicalRecordId = Id;
                    assessment.CreateUserId = assessment.UpdateUserId;
                    assessment.CreateDate = assessment.UpdateDate;

                    assessment_bll.Add(assessment);
                }
                else
                {
                    assessment_bll.Update(assessment);
                }
            }
            base.Redirect("操作成功！", "MedicalRecordEdit.aspx?Action=Detail&Id=" + Id.ToString());
            //base.Redirect("操作成功！");
        }
        catch (Exception ex)
        {
            base.Alert("操作失败！");
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (sender as LinkButton);
        string type = lbtn.CommandName;
        Response.Redirect("MedicalRecordEdit.aspx?Action=Detail&Id=" + Id + "&EditType=" + type + "#" + type);
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("MedicalRecordEdit.aspx?Action=Detail&Id=" + Id);
    }

    protected void btn_SelectGaugeType_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Gauge/SelectGaugeType.aspx?MedicalRecordId=" + Id);
    }

    #endregion


    #region 自定义方法
    private void ShowCheckBoxList(CheckBoxList cbl, string values)
    {
        string[] ClinicalDiagnosis = values.Split(',');
        foreach (string item in ClinicalDiagnosis)
        {
            if (cbl.Items.FindByText(item) != null)
            {
                cbl.Items.FindByText(item).Selected = true;
            }
        }
    }

    private string GetShowHtmlByCheckBoxList(CheckBoxList cbl)
    {
        string html = "";
        foreach (ListItem item in cbl.Items)
        {
            if (item.Selected)
            {
                html += "<span>" + item.Text + "</span>";
            }
        }
        return html;
    }


    private string GetCheckBoxListValues(CheckBoxList cbl)
    {
        string result = "";
        foreach (ListItem item in cbl.Items)
        {
            if (item.Selected)
            {
                if (result != "")
                {
                    result += ",";
                }
                result += item.Text;
            }
        }
        return result;
    }
    #endregion
}